const { defineConfig } = require("@vue/cli-service");
let { list } = require("./src/mock");
const bodyParser = require("body-parser");
const users = [
  {
    user: "zhangsan",
    pwd: "123456",
  },
  {
    user: "admin",
    pwd: "admin",
  },
];

module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    setupMiddlewares(middle, { app }) {
      app.use(bodyParser.json());

      app.get("/api/list", (req, res) => {
        // 获取参数
        const {
          pageCode = 1,
          pageSize = 10,
          orderState,
          id,
          tel,
          startDate,
          endDate,
        } = req.query;

        // 筛选
        const newList = list
          .filter((v) => {
            // 筛选订单状态
            // 全部订单返回所有数据
            if (orderState === "全部订单") return true;
            // 返回所选状态订单
            return v.orderState === orderState;
          })
          .filter((v) => {
            // 筛选编号
            return v.id.includes(id);
          })
          .filter((v) => {
            // 筛选联系方式
            return v.tel.includes(tel);
          })
          .filter((v) => {
            // 筛选时间
            if (startDate === "" || endDate === "") {
              return true;
            }
            // 规则： 开始时间 < 创建时间 && 结束时间 > 创建时间
            return (
              new Date(startDate).getTime() <=
                new Date(v.createDate).getTime() &&
              new Date(endDate).getTime() >= new Date(v.createDate).getTime()
            );
          });

        res.send({
          code: 200,
          data: newList.slice((pageCode - 1) * pageSize, pageCode * pageSize),
          total: newList.length,
        });
      });

      app.delete("/api/list", (req, res) => {
        const { id } = req.query;
        list = list.filter((v) => v.id !== id);
        res.send({
          code: 200,
          message: "删除成功！",
        });
      });

      app.delete("/api/lists", (req, res) => {
        const { ids } = req.query;
        list = list.filter((v) => !ids.includes(v.id));
        res.send({
          code: 200,
          message: "删除成功！",
        });
      });

      app.post("/api/list", (req, res) => {
        const { addOrder } = req.body;
        addOrder.id = Math.random().toString().slice(2);
        addOrder.code = Math.random().toString().slice(2);
        addOrder.createDate = new Date().toLocaleDateString();
        list.unshift(addOrder);
        res.send({
          code: 200,
          message: "添加成功",
        });
      });

      app.post("/api/list/edit", (req, res) => {
        const { currEditItem } = req.body;

        const item = list.find((v) => v.id === currEditItem.id);

        Object.assign(item, currEditItem);

        res.send({
          code: 200,
          message: "更新成功！！",
        });
      });

      app.post("/api/signin", (req, res) => {
        const { user, pwd } = req.body;

        const isExist = users.some((v) => v.user === user && v.pwd === pwd);

        if (isExist) {
          res.send({
            code: 200,
            message: "登录成功",
            data: {
              token: Math.random(),
            },
          });
        } else {
          res.send({
            code: 401,
            message: "账户密码错误， 请重新输入",
          });
        }
      });

      app.get("/api/echarts/order/state", (req, res) => {
        const data = ["待付款", "待发货", "已发货", "已完成", "已关闭"];
        const countData = [0, 0, 0, 0, 0];
        list.forEach(v => {
          const index = data.findIndex(item => v.orderState === item)
          countData[index]++
        })
        res.send({
          code: 200,
          data: {
            title: {
              text: "订单状态",
            },
            tooltip: {},
            xAxis: {
              data: data,
            },
            yAxis: {},
            series: [
              {
                name: "订单数量",
                type: "bar",
                data: countData,
              },
            ],
          },
        });
      });

      return middle;
    },
  },
});
